<?php include('header.php');?>
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span 12">
			<h1>ADMIN</h1>
		</div>
	</div>
	<div class="row-fluid">
		<div class="span2"">
			<?php include('lefter.php');?>
		</div>
		<div class="span10">
			<div class="row-fluid">
				<div class="span8">
					<h3>
						<span>WORK EDIT</span>
					</h3>
				</div>
			</div>
			<hr />
			<form id="view_form" class="form-horizontal"  action="<?=base_url('admin/work_edit_action')?>" method="post" enctype="multipart/form-data">
			<input type="hidden" name="id" value="<?=$work_view_row['id']?> "/>
			<input type="hidden" name="limit" value="<?=$limit?>" />
			<input type="hidden" name="offset" value="<?=$offset?>" />
			<div class="control-group">
				<label class="control-label" for="new_view"><strong>New 마크</strong></label>
				<div class="controls">
					<label class="radio inline"> 
						<input type="radio" name="new_view" id="new_view_show" value="show" <?php if($work_view_row['new_view'] == 'show') :?>checked<?php endif;?> /> 있음
					</label> 
					<label class="radio inline"> 
						<input type="radio" name="new_view" id="new_view_hide" value="hide" <?php if($work_view_row['new_view'] == 'hide') :?>checked<?php endif;?> /> 없음
					</label>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="security"><strong>보안잠금</strong></label>
				<div class="controls">
					<label class="radio inline"> 
						<input type="radio" name="security" id="security_open" value="open" <?php if($work_view_row['security'] == 'open') :?>checked<?php endif;?> /> 열림
					</label> 
					<label class="radio inline"> 
						<input type="radio" name="security" id="security_lock" value="lock" <?php if($work_view_row['security'] == 'lock') :?>checked<?php endif;?> /> 잠금
					</label>
					<input type="text" name="password" id="password" value="<?=$work_view_row['password']?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="title1"><strong>제목1</strong></label>
				<div class="controls">
					<input type="text" name="title1" id="title1" value="<?=$work_view_row['title1']?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="title2"><strong>제목2</strong></label>
				<div class="controls">
					<input type="text" name="title2" id="title2" value="<?=$work_view_row['title2']?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="client"><strong>클라이언트</strong></label>
				<div class="controls">
					<input type="text" name="client" id="client" value="<?=$work_view_row['client']?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="category_id"><strong>카테고리</strong></label>
				<div class="controls">
				<?php foreach($category_rows as $row) :?>
					<label class="checkbox inline"> 
						<input type="checkbox" name="category_id_<?=$row['id']?>" value="<?=$row['id']?>" <?php if(in_array($row['id'], $work_view_row['category_id'])) :?>checked<?php endif;?> /> <?=$row['title']?>
					</label>
				<?php endforeach;?>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label"><strong>썸네일</strong></label>
				<div class="controls">
					<div id="select_image_div">
					<?php foreach($work_view_row['image'] as $row) :?>
					<div class="fileupload fileupload-exists" data-provides="fileupload">
						<input type="hidden" name="<?=$row['id']?>" value="<?=$row['id']?>" />
						<div class="fileupload-preview thumbnail save-image-div"><?php if(empty($row['is_image'])) :?><?=$row['orig_name']?><?php else :?><img src="<?=UPLOAD_DIR?>/<?=$row['file_name']?>" /><?php endif;?></div>   
						<div>
							<a href="#" class="btn fileupload-exists btn-image-remove" data-dismiss="fileupload">Remove</a>
						</div>  
					</div>
					<?php endforeach;?>
					</div>
					<input type="hidden" name="image_count" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="date"><strong>날짜(화면표시)</strong></label>
				<div class="controls">
					<input type="text" name="date" id="date"  value="<?php if($work_view_row['date'] != '0000-00-00') :?><?=date("Y.m", strtotime($work_view_row['date']))?><?php endif;?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="tag"><strong>Tag</strong></label>
				<div class="controls">
					<input type="text" name="tag" id="tag" value="<?=$work_view_row['tag']?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="content"><strong>본문</strong></label>
				<div class="controls">
					<textarea id="content" name="content"><?=$work_view_row['content']?></textarea>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="relation_id"><strong>관련 프로젝트</strong></label>
				<div class="controls">
					<table>
						<tbody>
							<tr>
							<?php foreach($relation as $row) :?>
								<td>
									<div class="fileupload fileupload-exists" data-provides="fileupload">
										<input type="hidden" name="<?=$row['name']?>" value="<?=$row['id']?>" />
										<div class="fileupload-preview thumbnail save-image-div"><?php if(empty($row['image_is_image'])) :?><?=$row['image_orig_name']?><?php else :?><img src="<?=UPLOAD_DIR?>/<?=$row['image_file_name']?>" /><?php endif;?></div>   
										<div>
											<a href="#view" class="btn fileupload-exists btn-select"  data-toggle="modal" data-dismiss="fileupload">Select</a>
										</div>  
									</div>
								</td>
							<?php endforeach;?>
							</tr>
						</tbody>
					</table>
					<div id="view" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="view_label" aria-hidden="true">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
							<h3 id="view_label" class="view-label"></h3>
							<div class="btn-group" data-toggle="buttons-radio">
								<button type="button" class="btn active select-work">WORK</button>
							</div>
						</div>
						<div class="modal-body">
							<div id="view_list"></div>
						</div>
						<div class="modal-footer">
							<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
						</div>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="span4 offset8">
					<div class="pull-right">
						<button type="button" class="btn btn-cancel">Cancel</button>
						<button type="submit" class="btn btn-primary">Save</button>
					</div>
				</div>
			</div>
			</form>
		</div>
	</div>
</div>
<script src="<?=JS_DIR?>/jquery-1.8.3.min.js"></script>
<script src="<?=JS_DIR?>/respond.min.js"></script>
<script src="<?=BS_DIR?>/admin/js/bootstrap.min.js"></script>
<script src="<?=JS_DIR?>/jquery.tmpl.min.js"></script>
<script src="<?=BS_DIR?>/admin/js/bootstrap-fileupload.min.js"></script>
<script src="<?=JU_DIR?>/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="<?=CK_DIR?>/ckeditor.js"></script>
<script>
var limit = 5;
var relation_name = "";

var select_image_object = {
	number: 1,
	add: function() {
		$("input[name='image_count']").val(select_image_object.number);
		var data = {name: select_image_object.number};
		$("#select_image").tmpl(data).appendTo("#select_image_div");
		select_image_object.number++;
	}
};

function view_work(offset) {
	$("#view_list").empty();
	$.ajax({
		type: 'post',
		url: "<?=base_url('admin/main_view_work')?>/"+limit+"/"+offset,
		success: function(data){
			$("#view_label").html("SELECT");
			$("#select_work").tmpl(data).appendTo("#view_list");
		},
		error: function(jqXHR, textStatus, errorThrown){
			alert( errorThrown );
		}
	});
}

$(function() {
	<?php if(empty($work_view_row['image'])) :?>select_image_object.add();<?php endif;?>
	$("a[data-toggle='modal']").click(function() {
		relation_name = $(this).parent().parent().children("input").attr("name");
		view_work(0);
	});
	$("#date").datepicker({dateFormat: "yy.mm"});
	$("#view_form").on("submit", function() {
		if(!$("#title1").val()) {
			alert("제목1을 입력해 주세요");
			$("#title1").focus();
			return false;
		}
	});
	$(document).on("click", "a.btn-image-remove", function() {
		select_image_object.add();
		$(this).parent().parent().remove();
	});
	$("button.btn-cancel").click(function() {
		location.href = "<?=base_url('admin/work/'.$limit.'/'.$offset)?>";
	});
	CKEDITOR.replace( 'content', {
	    filebrowserUploadUrl: "<?=base_url('admin/ckeditor_image_upload')?>"
	});
	$(document).on("click", "button.btn-view-select", function() {
		if($(this).parent().parent().children(".thumb").children(".thumb-text").text()) {
			$("input[name='"+relation_name+"']").parent().children(".fileupload-preview").text($(this).parent().parent().children(".thumb").children(".thumb-text").text());
		}
		else if($(this).parent().parent().children(".thumb").children(".thumb-image").html()) {
			$("input[name='"+relation_name+"']").parent().children(".fileupload-preview").html($(this).parent().parent().children(".thumb").children(".thumb-image").html());
		}
		$("input[name='"+relation_name+"']").val($(this).attr("data-view_id"));
		$("#view").modal('hide');
	});
});
</script>
<script id="select_image" type="text/x-jquery-tmpl">
    <div class="fileupload fileupload-new" data-provides="fileupload">
		<div class="fileupload-preview thumbnail"></div>
		<div>
			<span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" name="image_${name}"/></span>
			<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
		</div>
	</div>
</script>
<script id="select_work" type="text/x-jquery-tmpl">
<div class="ul-table work">
	<ul class="header">
		<li class="no">NO</li>
		<li class="thumb hidden-phone">THUMB</li>
		<li class="date hidden-phone">DATE</li>
		<li class="title">TITLE</li>
		<li class="delete">SELECT</li>
	</ul>
</div>
<div class="ul-table ul-table-body work">
	{{tmpl(work_rows) "#select_work_list"}}
</div>
<div class="pagination pagination-centered">
	<ul>
		{{html pagination_links}}
	</ul>
</div>
</script>
<script id="select_work_list" type="text/x-jquery-tmpl">
<ul class="image-view">
	<li class="no">${id}</li>
	<li class="thumb hidden-phone">{{if !image_is_image}}<div class="thumb-text">${image_orig_name}</div>{{else}}<div class="thumb-image"><img src="<?=UPLOAD_DIR?>/${image_file_name}" /></div>{{/if}}</li>
	<li class="date hidden-phone">{{if date}}${date}{{else}}&nbsp;{{/if}}</li>
	<li class="title">${title1}</li>
	<li class="delete"><button type="button" class="btn btn-small btn-danger btn-view-select" data-view_kind="work" data-view_id="${id}">SELECT</button></li>
</ul>
</script>
<?php include('footer.php');?>